package com.example.base.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.security.SecurityUtil;
import com.example.base.security.SysUser;
import com.example.base.security.domain.SysUserEntity;
import com.example.base.security.domain.SysRoleEntity;
import com.example.base.security.domain.vo.SysRoleVo;
import com.example.base.security.mapper.SysRoleMapper;
import com.example.base.security.service.SysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/11 14:49
 * @since JDK 1.8
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRoleEntity> implements SysRoleService {

    @Override
    public SysRoleEntity addOrUpdateSysRole(SysRoleVo vo) throws GlobalExcetion {
        SysRoleEntity sysRoleEntity=new SysRoleEntity();
        BeanUtils.copyProperties(vo,sysRoleEntity);
        SysUser selfUserEntity= SecurityUtil.getSysUserEntity();
        sysRoleEntity.setCreateUser(selfUserEntity.getUserId().toString());
        if(vo.getRoleId()!=null){
            SysRoleEntity entity=getById(vo.getRoleId());
            if(entity==null){
                throw new GlobalExcetion(1,"角色不存在");
            }
            if(!entity.getRoleno().equals(vo.getRoleno())){
                SysRoleEntity roleEntity=getSysRoleEntityByRoleNo(vo.getRoleno());
                if(roleEntity!=null){
                    throw new GlobalExcetion(1,"角色no已存在");
                }
            }
            this.baseMapper.updateById(sysRoleEntity);
        }else {
            SysRoleEntity roleEntity=getSysRoleEntityByRoleNo(vo.getRoleno());
            if(roleEntity!=null){
                throw new GlobalExcetion(1,"角色no已存在");
            }
            this.baseMapper.insert(sysRoleEntity);
        }
        return sysRoleEntity;
    }

    @Override
    public SysRoleEntity getSysRoleEntityByRoleNo(String roleNo) {
        LambdaQueryWrapper<SysRoleEntity> sysRoleEntityLambdaQueryWrapper=new LambdaQueryWrapper<>();
        sysRoleEntityLambdaQueryWrapper.eq(SysRoleEntity::getRoleno,roleNo);
        return super.baseMapper.selectOne(sysRoleEntityLambdaQueryWrapper);
    }



    @Override
    public IPage<SysRoleEntity> page(Integer pageNum, Integer pageSize, String keyword) {
        LambdaQueryWrapper<SysRoleEntity> sysRoleEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(keyword)){
            sysRoleEntityLambdaQueryWrapper.like(SysRoleEntity::getRoleno,keyword);
            sysRoleEntityLambdaQueryWrapper.like(SysRoleEntity::getRoleName,keyword);
        }
        sysRoleEntityLambdaQueryWrapper.orderByDesc(SysRoleEntity::getCreateTime);
        IPage<SysRoleEntity> page=this.baseMapper.selectPage(new Page(pageNum,pageSize),sysRoleEntityLambdaQueryWrapper);
        return page;
    }
}
